package com.ruoyi.project.pc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.JavaBeanUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.pc.domain.Advertisement;
import com.ruoyi.project.pc.domain.ImageNumber;
import com.ruoyi.project.pc.service.IAdvertisementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 广告、小程序首页图片Controller
 * 
 * @author luaoran
 * @date 2023-06-05
 */
@RestController
@RequestMapping("/system/advertisement")
@Api(value = "广告、小程序首页图片管理",tags = "广告、轮播图图片管理")
public class AdvertisementController extends BaseController
{
    @Autowired
    private IAdvertisementService advertisementService;


    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询首页轮播图图片列表
     */
    @ApiOperation("获取轮播图、广告图片")
    @GetMapping("/list")
    public AjaxResult list(Advertisement advertisement)
    {
        List<Advertisement> list = advertisementService.selectAdvertisementList(advertisement);
        return AjaxResult.success(list);
    }

    /**
     * 查询首页轮播图图片列表
     */
    @ApiOperation("小程序获取轮播图、广告图片")
    @GetMapping("/wx")
    public AjaxResult listByWx(Advertisement advertisement)
    {
        List<Advertisement> list = advertisementService.selectAdvertisementList(advertisement);
        return AjaxResult.success(list);
    }



    /**
     * 获取广告、小程序首页图片详细信息
     */
    @ApiOperation("根据Id查看公司详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(advertisementService.selectAdvertisementById(id));
    }

    /**
     * 新增广告、小程序首页图片
     */

    @Log(title = "广告、小程序首页图片", businessType = BusinessType.INSERT)
    @ApiOperation("新增轮播图、广告图片")
    @PostMapping
    public AjaxResult add(@RequestParam MultipartFile image, String advertise) throws Exception
    {
        Assert.isTrue(!image.isEmpty(),"图片不能为空");
        JSONObject jsonObject = JSONObject.parseObject(advertise);
        Advertisement advertisement = new Advertisement();
        Object type = jsonObject.get("type");
        Assert.notNull(type,"图片类型不能为空");
//        if ("1".equals(type.toString())){
//            Object title = jsonObject.get("title");
//            Assert.notNull(title,"图片标题不能为空");
//            Object description = jsonObject.get("description");
//            Assert.notNull(description,"图片内容不能为空");
//            advertisement.setTitle(title.toString());
//            advertisement.setDescription(description.toString());
//        }
        // 上传文件路径z
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, image);
        String url = serverConfig.getUrl()  + fileName;
        advertisement.setUrl(url);

        advertisement.setType(type.toString());
        advertisement.setCreateBy(SecurityUtils.getUsername());
        return toAjax(advertisementService.insertAdvertisement(advertisement));
    }


    @ApiOperation("新增通知文本内容")
    @PostMapping("/notice")
    public AjaxResult addNotice(@RequestBody Advertisement advertisement) throws Exception
    {
        return toAjax(advertisementService.insertAdvertisement(advertisement));
    }

    /**
     * 修改广告、小程序首页图片
     */

    @Log(title = "广告、小程序首页图片", businessType = BusinessType.UPDATE)
    @ApiOperation("修改公司图文信息")
    @PostMapping("/update")
    public AjaxResult edit(@RequestParam(value = "image",required = false)MultipartFile image, String advertise) throws Exception
    {
        JSONObject jsonObject = JSONObject.parseObject(advertise);
        Advertisement advertisement = JavaBeanUtil.fastToBean(jsonObject, Advertisement.class);
        advertisement.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(advertisementService.updateAdvertisement(advertisement,image));
    }

    @PutMapping("/edit-number")
    public AjaxResult editNumber(@RequestBody Advertisement advertisement){
        return AjaxResult.success(advertisementService.editNumber(advertisement));

    }

    /**
     * 删除广告、小程序首页图片
     */

    @Log(title = "广告、小程序首页图片", businessType = BusinessType.DELETE)
    @ApiOperation("删除图片")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(advertisementService.deleteAdvertisementByIds(ids));
    }

    /**
     * 批量修改图片顺序
     */

    @Log(title = "广告、小程序首页图片", businessType = BusinessType.UPDATE)
    @ApiOperation("修改图片顺序")
    @PostMapping("/update-number")
    public AjaxResult update(@RequestBody ImageNumber imageNumber) {
        String imageList = imageNumber.getImageList();
        List<Advertisement> advertisements = JSON.parseArray(imageList, Advertisement.class);
        return toAjax(advertisementService.updateNumber(advertisements));
    }

    @ApiOperation("修改")
    @PutMapping("")
    public AjaxResult put(@RequestBody Advertisement advertisement) {
        return toAjax(advertisementService.editNumber(advertisement));
    }
}
